package com.ymatou.iapi.trading.userquery.testcase;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Lists;
import com.ymatou.iapi.synctrade.parameter.mongo.Order;
import com.ymatou.iapi.trading.userquery.parameter.resp.BuyerOrderInfo;
import com.ymatou.iapi.trading.userquery.parameter.resp.GetBuyerOrderListResp;
import com.ymatou.iapi.trading.userquery.service.GetBuyerOrderListCall;
import com.ymatou.query.trading.api.parameter.GetBuyerOrderListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.mongo.OrderQueryWapper;
import com.ymttest.database.mongo.UserIndexQueryWapper;
import com.ymttest.database.mongo.config.UserIndexField;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 根据订单状态查询相关订单信息列表（带分页） FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetBuyerOrderList {

	private static GetBuyerOrderListBean getbuyerorderlistbean;
	private static GetBuyerOrderListCall getbuyerorderlistcall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据订单状态查询相关订单信息列表（带分页）");
	}

	@Before
	public void caseUp() {
		getbuyerorderlistbean = new GetBuyerOrderListBean();
		getbuyerorderlistcall = new GetBuyerOrderListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getbuyerorderlistcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void constructExpectedResult(GetBuyerOrderListResp resp) {

		List<Criteria> criteriaList = Lists.newArrayList(Criteria
				.where(UserIndexField.USER_ID)
				.is(getbuyerorderlistbean.getBuyerId())
				.norOperator(Criteria.where(UserIndexField.HUNTER_ID).gt(0))
				, Criteria.where(UserIndexField.DELETED).is(false));
		
		if (!CollectionUtils.isEmpty(getbuyerorderlistbean.getOrderStatusList()))
			criteriaList.add(Criteria.where(UserIndexField.STATUS).in(getbuyerorderlistbean.getOrderStatusList()));
		
		Criteria criteria = new Criteria().andOperator(criteriaList.toArray(new Criteria[0]));
		Query query = Query.query(criteria);
		
		query.with(new Sort(Sort.Direction.DESC, UserIndexField.ADD_TIME));
		query.limit(getbuyerorderlistbean.getPageSize());

		List<Long> orderIds = new UserIndexQueryWapper().queryOrderId(query, getbuyerorderlistbean.getBuyerId());
		List<BuyerOrderInfo> orderInfoList = Lists.newArrayList();
		
		if (!CollectionUtils.isEmpty(orderIds)) {
			
			List<Order> orders = new OrderQueryWapper().findByIds(orderIds);
			
			// 按主单号降序排序
			Collections.sort(orders,
					(o1, o2) -> o2.getAddTime().compareTo(o1.getAddTime()));
			
			orders.forEach(o -> {
				
				BuyerOrderInfo orderInfo = new BuyerOrderInfo();
				
				orderInfo.setOrderId(o.getId());
				orderInfo.setAddTime(o.getAddTime());
				orderInfo.setTradingStatus(o.getTradingStatus());
				orderInfo.setBillCodeList(null != o.getDelivery() ? o.getDelivery().getBillCode() : null);
			});
		}
		
		resp.setOrderInfoList(orderInfoList);
	}

	private void verify() {

		GetBuyerOrderListResp expectResponse = new GetBuyerOrderListResp();
		constructExpectedResult(expectResponse);
		GetBuyerOrderListResp actualResponse = getbuyerorderlistcall
				.getBuyerGroupOrderInfosResp();

		List<BuyerOrderInfo> expectOrders = expectResponse.getOrderInfoList();
		List<BuyerOrderInfo> actualOrders = actualResponse.getOrderInfoList();

		actualOrders.sort((a, b) -> {
			if (a.getOrderId() > b.getOrderId())
				return -1;
			else {
				return 1;
			}
		});

		for (int i = 0; i < expectOrders.size(); i++) {
			BuyerOrderInfo expect = expectOrders.get(i);
			BuyerOrderInfo actual = actualOrders.get(i);

			DataUtil.verify(expect.getOrderId(), actual.getOrderId(),
					"验证 BuyerOrderInfo.orderId");
			DataUtil.verify(expect.getTradingStatus(),
					actual.getTradingStatus(),
					"验证 BuyerOrderInfo.tradingStatus");
			DataUtil.verify(expect.getAddTime(), actual.getAddTime(),
					"验证 BuyerOrderInfo.addTime");
			DataUtil.verify(expect.getBillCodeList().size() == 0
					? null
					: expect.getBillCodeList(), actual.getBillCodeList(),
					"验证 BuyerOrderInfo.billCodeList");

		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderList_001() {
		Logger.start(true, "根据订单状态查询成功");
		try {
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));

			getbuyerorderlistbean.setBuyerId(userId);
			// 当前页码数
			int CurrentPageNum = 1;
			getbuyerorderlistbean.setCurrentPageNum(CurrentPageNum);
			// 订单状态
			ArrayList<Integer> orderStatus = new ArrayList<Integer>();
			orderStatus.add(1);
			getbuyerorderlistbean.setOrderStatusList(orderStatus);
			// 页面显示记录条数
			getbuyerorderlistbean.setPageSize(5);

			// 发送请求
			getbuyerorderlistcall.setData(getbuyerorderlistbean);
			getbuyerorderlistcall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getbuyerorderlistcall.getCode(),
					"验证Code");

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderList_002() {
		Logger.start(true, "根据订单状态查询查询成功");
		try {
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));

			getbuyerorderlistbean.setBuyerId(userId);
			// 当前页码数
			int CurrentPageNum = 1;
			getbuyerorderlistbean.setCurrentPageNum(CurrentPageNum);
			// 订单状态
			ArrayList<Integer> orderStatus = new ArrayList<Integer>();
			orderStatus.add(4);
			getbuyerorderlistbean.setOrderStatusList(orderStatus);
			// 页面显示记录条数
			getbuyerorderlistbean.setPageSize(30);
			// 发送请求
			getbuyerorderlistcall.setData(getbuyerorderlistbean);
			getbuyerorderlistcall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getbuyerorderlistcall.getCode(),
					"验证Code");

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderList_003() {
		Logger.start(true, "根据订单状态查询成功");
		try {
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));

			getbuyerorderlistbean.setBuyerId(userId);
			// 当前页码数
			int CurrentPageNum = 1;
			getbuyerorderlistbean.setCurrentPageNum(CurrentPageNum);
			// 订单状态
			ArrayList<Integer> orderStatus = new ArrayList<Integer>();
			orderStatus.add(1);
			orderStatus.add(2);
			orderStatus.add(3);
			orderStatus.add(4);
			orderStatus.add(12);
			orderStatus.add(13);
			orderStatus.add(16);
			orderStatus.add(17);
			orderStatus.add(18);
			getbuyerorderlistbean.setOrderStatusList(orderStatus);
			// 页面显示记录条数
			getbuyerorderlistbean.setPageSize(20);
			// 发送请求
			getbuyerorderlistcall.setData(getbuyerorderlistbean);
			getbuyerorderlistcall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getbuyerorderlistcall.getCode(),
					"验证Code");

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderList_004() {
		Logger.start(true, "根据订单状态查询成功,当前不为第一页");
		try {
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));

			getbuyerorderlistbean.setBuyerId(userId);
			// 当前页码数
			int CurrentPageNum = 1;
			getbuyerorderlistbean.setCurrentPageNum(CurrentPageNum);
			// 订单状态
			ArrayList<Integer> orderStatus = new ArrayList<Integer>();
			//orderStatus.add(1);
			orderStatus.add(2);
			orderStatus.add(3);
			orderStatus.add(4);
			orderStatus.add(12);
			orderStatus.add(13);
			orderStatus.add(16);
			orderStatus.add(17);
			orderStatus.add(18);
			getbuyerorderlistbean.setOrderStatusList(orderStatus);
			// 页面显示记录条数
			getbuyerorderlistbean.setPageSize(20);
			// 发送请求
			getbuyerorderlistcall.setData(getbuyerorderlistbean);
			getbuyerorderlistcall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getbuyerorderlistcall.getCode(),
					"验证Code");

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderList_005() {
		Logger.start(true, "根据订单状态查询成功");
		try {
			int userId = 20325187;

			getbuyerorderlistbean.setBuyerId(userId);
			// 当前页码数
			int CurrentPageNum = 1;
			getbuyerorderlistbean.setCurrentPageNum(CurrentPageNum);
			// 订单状态
			ArrayList<Integer> orderStatus = new ArrayList<Integer>();
			orderStatus.add(2);
			getbuyerorderlistbean.setOrderStatusList(orderStatus);
			// 页面显示记录条数
			getbuyerorderlistbean.setPageSize(5);

			// 发送请求
			getbuyerorderlistcall.setData(getbuyerorderlistbean);
			getbuyerorderlistcall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getbuyerorderlistcall.getCode(),
					"验证Code");

			//			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}